{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单轮会话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html\n",
    "from http import HTTPStatus\n",
    "import dashscope\n",
    "dashscope.api_key = \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "def call_with_messages():\n",
    "    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},\n",
    "                {'role': 'user', 'content': '如何做炒西红柿鸡蛋？'}]\n",
    "\n",
    "    response = dashscope.Generation.call(\n",
    "        dashscope.Generation.Models.qwen_turbo,\n",
    "        messages=messages,\n",
    "        result_format='message',  # set the result to be \"message\" format.\n",
    "    )\n",
    "    if response.status_code == HTTPStatus.OK:\n",
    "        print(response)\n",
    "    else:\n",
    "        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (\n",
    "            response.request_id, response.status_code,\n",
    "            response.code, response.message\n",
    "        ))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    call_with_messages()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 返回结果示例\n",
    "```{\n",
    "    \"status_code\": 200,\n",
    "    \"request_id\": \"05dc83af-7185-9e14-9b0b-4466de159d6a\",\n",
    "    \"code\": \"\",\n",
    "    \"message\": \"\",\n",
    "    \"output\": {\n",
    "        \"text\": null,\n",
    "        \"finish_reason\": null,\n",
    "        \"choices\": [\n",
    "            {\n",
    "                \"finish_reason\": \"stop\",\n",
    "                \"message\": {\n",
    "                    \"role\": \"assistant\",\n",
    "                    \"content\": \"首先，准备两个鸡蛋，一个西红柿，适量的盐、糖、料酒和生抽。将鸡蛋打入碗中，搅拌均匀，西红柿切块。锅中加油，油热后加入鸡蛋液，炒至金黄色，盛出备用。锅中加油，油热后加入西红柿块，翻炒均匀，加入适量的盐、糖、料酒和生抽，炒至西红柿软烂，加入炒好的鸡蛋，翻炒均匀即可。\"\n",
    "                }\n",
    "            }\n",
    "        ]\n",
    "    },\n",
    "    \"usage\": {\n",
    "        \"input_tokens\": 12,\n",
    "        \"output_tokens\": 98,\n",
    "        \"total_tokens\": 110\n",
    "    }\n",
    "}````"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.多轮会话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"status_code\": 200, \"request_id\": \"89f6f433-164e-9db0-893a-8e833bf62819\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"stop\", \"message\": {\"role\": \"assistant\", \"content\": \"材料：牛腩1000克，西红柿2个，大葱1根，姜3片，蒜瓣6粒，八角1颗，桂皮1小块，香叶2片，干辣椒2只，生抽2勺，老抽1勺，料酒2勺，冰糖20克，盐适量\\n\\n做法：\\n\\n1. 牛腩洗净切块，放沸水中焯水去血沫捞出备用。\\n2. 西红柿顶部划十字刀口，放入沸水中烫一下去皮，切滚刀块备用。\\n3. 大葱切段、姜切片、蒜瓣剥好。锅内油烧热，下入大葱段、姜片、蒜瓣、八角、桂皮、香叶和干辣椒炒香。\\n4. 下入牛腩翻炒均匀，烹入料酒炒匀后，加入生抽和老抽炒至上色。\\n5. 加入足量热水（没过牛腩），大火煮开后撇去浮沫，转中小火慢慢炖煮至牛腩软烂，约需1小时以上。\\n6. 当牛腩炖至8成熟时，加入西红柿块继续炖煮，炖煮至西红柿软烂且汤汁浓稠即可，最后调入适量的盐调味。\\n\\n西红柿炖牛腩就做好了！\"}}]}, \"usage\": {\"input_tokens\": 13, \"output_tokens\": 286, \"total_tokens\": 299}}\n",
      "{\"status_code\": 200, \"request_id\": \"bb0bfd1a-b657-9126-8b6f-99832ab72b11\", \"code\": \"\", \"message\": \"\", \"output\": {\"text\": null, \"finish_reason\": null, \"choices\": [{\"finish_reason\": \"stop\", \"message\": {\"role\": \"assistant\", \"content\": \"可以的，如果不喜欢甜味，可以不放冰糖或者减少用量。不过需要注意的是，糖能够增加菜肴的鲜美口感和色泽，如果你不放糖，可能会让菜肴的味道变得有些平淡无味。另外，糖还能够中和牛肉的腥味，如果没有糖的话，可能需要在炖煮的过程中多加一些调料来提味。\"}}]}, \"usage\": {\"input_tokens\": 305, \"output_tokens\": 77, \"total_tokens\": 382}}\n"
     ]
    }
   ],
   "source": [
    "from http import HTTPStatus\n",
    "from dashscope import Generation\n",
    "from dashscope.api_entities.dashscope_response import Role\n",
    "\n",
    "dashscope.api_key = \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "def conversation_with_messages():\n",
    "    messages = [{'role': Role.SYSTEM, 'content': 'You are a helpful assistant.'},\n",
    "                {'role': Role.USER, 'content': '如何做西红柿炖牛腩？'}]\n",
    "    response = Generation.call(\n",
    "        Generation.Models.qwen_turbo,\n",
    "        messages=messages,\n",
    "        result_format='message',  # set the result to be \"message\" format.\n",
    "    )\n",
    "    if response.status_code == HTTPStatus.OK:\n",
    "        print(response)\n",
    "        # append result to messages.\n",
    "        messages.append({'role': response.output.choices[0]['message']['role'],\n",
    "                         'content': response.output.choices[0]['message']['content']})\n",
    "    else:\n",
    "        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (\n",
    "            response.request_id, response.status_code,\n",
    "            response.code, response.message\n",
    "        ))\n",
    "    messages.append({'role': Role.USER, 'content': '不放糖可以吗？'})\n",
    "    # make second round call\n",
    "    response = Generation.call(\n",
    "        Generation.Models.qwen_turbo,\n",
    "        messages=messages,\n",
    "        result_format='message',  # set the result to be \"message\" format.\n",
    "    )\n",
    "    if response.status_code == HTTPStatus.OK:\n",
    "        print(response)\n",
    "    else:\n",
    "        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (\n",
    "            response.request_id, response.status_code,\n",
    "            response.code, response.message\n",
    "        ))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    conversation_with_messages()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "eec67444d00d22f71b39bd6bbad710a72749a23d029a63a184fb88e8d46e38e1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
